<%@ page import="com.ychs.entity.College" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    //获取查询页面提交过来的修改数据
    String id = request.getParameter("id");
    String name = request.getParameter("name");
    String code = request.getParameter("code");
    College college = new College();
    college.setId(Integer.parseInt(id));
    college.setName(name);
    college.setCode(Integer.parseInt(code));
    pageContext.setAttribute("college",college);
%>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="layui-elem-field layui-field-title">
            <legend>添加院系</legend>
        </fieldset>
        <form class="layui-form" action="CollegeServlet" method="post"
                    id="collegeModifyFrom" lay-filter="collegeModifyFormFilter">
            <%--提交方式，隐藏域--%>
            <input type="hidden" name="method" value="modify">
                <%--被修改院系的ID--%>
            <input type="hidden" id="id" name="id">

            <div class="layui-form-item">
                <label class="layui-form-label">院系名称<span style="color: red">*</span></label>
                <div class="layui-input-block">
                    <input type="text" class="layui-input"
                           id="name" name="name"
                           lay-verify="name" placeholder="请输入院系名称" lay-reqtext="院系名称为必填项">
                </div>
            </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">院系编码<span style="color: red">*</span></label>
                    <div class="layui-input-block">
                        <input type="text" class="layui-input"
                               id="code" name="code"
                               lay-verify="required|code" placeholder="请输入学院编码"
                               autocomplete="off" lay-reqtext="院系编码为必填项">
                    </div>
                </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="button" id="collegeSaveBtn" class="layui-btn " lay-submit="" lay-filter="collegeSaveBtn">修改院系</button>
                    <button type="button" id="resetBtn" class="layui-btn " lay-filter="resetBtn">重置</button>
                </div>
            </div>
        </form>

    </div>
</div>
<script>
    layui.use(['form','layedit'],function () {

        var form=layui.form,
            layedit=layui.layedit,
            layer=layui.layer,
            $ = layui.$;

        form.render();

        function formInit(){
            form.val('collegeModifyFormFilter',{
                "id":'${college.id}',
                "name":'${college.name}',
                "code":'${college.code}'
            });
        }

        formInit();


        $("#name").blur(function () {
            var value = this.value;
            if (typeof value == "undefined" || value == null || value == "") {
                layer.msg("院系名称为必填项！", {icon: 5});
            } else if (/^[\u4e00-\u9fa5]{2,15}$/.test(value) == false) {
                layer.msg("学院名或系名为 2-15 个汉字!");
            }
        })

        $("#code").blur(function () {
            var value = this.value;
            var reg = /^\d{1,11}$/;
            if (typeof value == "undefined" || value == null || value == "") {
                layer.msg("院系编号为必填项！", {icon: 5});
            } else if (reg.test(value) == false) {
                layer.msg("院系代号为1-11 位的数字!", {icon: 5});
            }
        })

        form.verify({
            name: function (value) {
                if (/^[\u4e00-\u9fa5]{2,15}$/.test(value) == false) {
                    return '学院名或系名为 2-15 个汉字'
                }

            },
            //不能有特殊字符和数字；可以输入英文，可以有空格，可以输入英文名字中的点，可以输入汉字，中文英文不能同时出现，长度在1-20
            code: function (value) {
                var reg = /^\d{1,11}$/;
                if (typeof value == "undefined" || value == null || value == "") {
                    return '院系编号为必填项！'
                } else if (reg.test(value) == false) {
                    return '院系代号为1-11 位的数字! '
                }
            },
        });

        //监听提交
        form.on('submit(collegeSaveBtn)', function (data) {

            //通过AJAX提交数据到后台，根据后台的反馈做下一步的处理
            let url = "CollegeServlet";
            $.post(url,{method:data.field.method,id:data.field.id,name:data.field.name,code:data.field.code},function (res) {

                if(res=='true'){
                    //在查询院系界面，关闭添加院系界面
                    layer.close(layer.index);
                    parent.layui.table.reload('currentTableId');
                    layer.msg("修改成功",{icon:1});
                }
            })
            //return false;
        });

        $('#resetBtn').click(function () {
            formInit();
            layer.msg("信息重置成功",{icon: 1});
        })

    })
</script>

</body>
</html>